package com.atguigu.eduservice.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-03-26
 */
@RestController
@RequestMapping("/eduservice/comment")
//@CrossOrigin
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    @GetMapping("{page}/{limit}")
    public R  getCommentPageList(@PathVariable long page, @PathVariable long limit,  String courseId){
        Page<EduComment> pageQuery = new Page<>(page, limit);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        commentService.page(pageQuery,wrapper);
        List<EduComment> records = pageQuery.getRecords();
        long current = pageQuery.getCurrent();
        boolean hasPrevious = pageQuery.hasPrevious();
        boolean hasNext = pageQuery.hasNext();
        long size = pageQuery.getSize();
        long total = pageQuery.getTotal();
        long pages = pageQuery.getPages();
        Map<String, Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("hasPrevious",hasPrevious);
        map.put("hasNext",hasNext);
        map.put("size",size);
        map.put("total",total);
        map.put("pages",pages);
        return R.ok().data(map);
    }
////根据课程id查询评论列表
//@ApiOperation(value = "评论分页列表")
//@GetMapping("{page}/{limit}")
//public R index(
//        @ApiParam(name = "page", value = "当前页码", required = true)
//        @PathVariable Long page,
//        @ApiParam(name = "limit", value = "每页记录数", required = true)
//         @PathVariable Long limit,
//         @ApiParam(name = "courseQuery", value = "查询对象", required = false) String courseId) {
//        Page<EduComment> pageParam = new Page<>(page, limit);
//        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
//        wrapper.eq("course_id",courseId);
//        commentService.page(pageParam,wrapper);
//        List<EduComment> commentList = pageParam.getRecords();
//        Map<String, Object> map = new HashMap<>();
//        map.put("items", commentList);
//        map.put("current", pageParam.getCurrent());
//        map.put("pages", pageParam.getPages());
//        map.put("size", pageParam.getSize());
//        map.put("total", pageParam.getTotal());
//        map.put("hasNext", pageParam.hasNext());
//        map.put("hasPrevious", pageParam.hasPrevious());
//        return R.ok().data(map);     }


    @PostMapping("auth/save")
    public R save(@RequestBody EduComment  comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)){
            return R.error().code(20001).message("请登录");
        }
        comment.setMemberId(memberId);
        Map map = ucenterClient.getcurrentInfo(comment.getMemberId());
        Object member = map.get("member");
//        EduComment member = (EduComment)map.get("member");
//        将object类型转换为json类型
        String memberlist = JSON.toJSONString(member);
//        再将json类型转换为相对应的类型
        EduComment eduComment = JSONObject.parseObject(memberlist, EduComment.class);
        comment.setNickname(eduComment.getNickname());
        comment.setAvatar(eduComment.getAvatar());
        commentService.save(comment);
        return R.ok();



    }

}

